package com.bert.base.controller.sys;

import java.util.Date;

import javax.annotation.Resource;
import java.util.List;

import com.bert.base.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.bert.base.entity.sys.SysUserEntity;
import com.bert.base.service.sys.SysUserService;
import tk.mybatis.mapper.entity.Condition;


/**
 * 系统用户
 * 
 * @author Bert
 * @date 2018-04-15 23:21:11
 */
@RestController
@RequestMapping("/sys/sysuser")
public class SysUserController {

	private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);

	@Resource
	private SysUserService sysUserService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public Result list(SysUserEntity sysUser, PageInfo<SysUserEntity> pageInfo){
		//查询列表数据
        List<SysUserEntity> sysUserList;
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
				sysUserList = sysUserService.select(sysUser);
            pageInfo = new PageInfo<SysUserEntity>(sysUserList);

        } catch (Exception e) {
            logger.error("查询系统用户列表失败！",e);
            return Result.error("查询系统用户列表失败！");
        }

		return Result.ok().put("page", pageInfo);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{userId}")
	public Result info(@PathVariable("userId") Long userId){

		SysUserEntity sysUser;
        try {
			sysUser = sysUserService.selectByPrimaryKey(userId);
        } catch (Exception e) {
            logger.error("查询系统用户信息失败！{}{}",userId,e);
            return Result.error("查询系统用户失败");
        }

		return Result.ok().put("sysUser", sysUser);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public Result save(@RequestBody SysUserEntity sysUser){
//		sysUserService.save(sysUser);

        try {
//            sysUser.setUpdateBy(getUserId());
//            sysUser.setCreateBy(getUserId());
			sysUserService.insertSelective(sysUser);
        } catch (Exception e) {
            logger.error("保存系统用户失败");
            return Result.error("保存系统用户失败");
        }

		return Result.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody SysUserEntity sysUser){

        try {
//            sysUser.setUpdateBy(getUserId());
            sysUser.setUpdateTime(new Date());
//            Condition condition = new Condition(SysRoleEntity.class);
//            condition.createCriteria().andEqualTo("updateTime",sysUser.getUpdateTime())
//                    .andEqualTo("userId",sysUser.getUserId.);
//			sysUserService.updateByCondition(sysUser,condition);
        } catch (Exception e) {
            logger.error("修改系统用户失败");
            return Result.error("修改系统用户失败");
        }
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public Result delete(@RequestBody Long[] userIds){
//		sysUserService.deleteBatch(userIds);
		
		return Result.ok();
	}
	
}
